<?php

namespace app\api\controller;

use think\Request;
use app\api\model\SpeciModel;
use app\api\validate\SpeciValidate;
use think\Db;

class Speci extends Common
{
    /**
     * 分类  列表
     * @param str $name 名称
     * @return Json
     */
    public function CategoryList(Request $request, SpeciModel $model)
    {
        $list = $model->getLi($request->param());
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function Cate(SpeciModel $model)
    {
        $list = $model->getCate();
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function CateInfo($id, SpeciModel $model)
    {
//        $list['info'] = $model->getInfo($id);
        $list = $model->getCate();
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 分类添加
     * @return Json
     */
    public function categoryAdd(Request $request, SpeciModel $model)
    {
        $data = $request->param();
        $data['pid'] = $request->param('pid', 0);
        $arr = $model->getCateAdd($data);
        if (!empty($arr)) {
            return $this->returnJson(200, 'ok',
                Db::name('category')->find(['id' => $arr]));
        } else {
            return json(['code' => 404, 'msg' => '添加失败']);
        }
    }

    /**
     * 分类编辑
     * @return Json
     */
    public function categoryEdit($id, Request $request, SpeciModel $model)
    {
        $data = $request->param();
        $data['default'] = 0;
        $arr = $model->getCateEdit($id, $data);
        if (!empty($arr)) {
            return json(['code' => 200, 'msg' => '修改成功']);
        } else {
            return json(['code' => 404, 'msg' => '修改失败']);
        }
    }

    /**
     * 分类删除
     * @param int $id 属性id
     * @return Json
     */
    public function CategoryDel($id, SpeciModel $model)
    {
        $attr = 'category';
        $arr = $model->getCateDel($id, $attr);
        if ($arr == 2) {
            return json(['code' => 404, 'msg' => '属性已绑定商品,无法删除']);
        } else {
            return json(['code' => 200, 'msg' => '成功']);
        }
    }
}
